package com.happy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.happy.dao.NoteMapper;
import com.happy.entity.NoteEntity;
import com.happy.service.INoteService;
import com.happy.util.StateEnum;
import com.happy.util.StateException;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Author: Wuzh
 * @Description:
 * @Date Created in 21:48 2021/1/27
 * @return
 */

@Service
public class NoteServiceImpl extends ServiceImpl<NoteMapper, NoteEntity> implements INoteService {

    @Resource
    private NoteMapper noteMapper;

    @Override
    public List<NoteEntity> queryList (NoteEntity noteEntity) {
        QueryWrapper<NoteEntity> queryWrapper = new QueryWrapper<> ();
        if ( noteEntity == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        if ( noteEntity.getId () != null ) {
            queryWrapper.eq ( "id", noteEntity.getId () );
        }
        if ( StringUtils.isNotBlank ( noteEntity.getNoteTitle () ) ) {
            queryWrapper.like ( "noteTitle", noteEntity.getNoteTitle () );
        }
        if ( noteEntity.getLevel () != null ) {
            queryWrapper.eq ( "level", noteEntity.getLevel () );
        }
        if ( noteEntity.getCategoryId () != null ) {
            queryWrapper.eq ( "categoryId", noteEntity.getCategoryId () );
        }
//        List<NoteEntity> noteEntities = baseMapper.selectList ( queryWrapper );
        List<NoteEntity> noteEntities = noteMapper.queryList ( 2 );
        return noteEntities;
    }

    @Override
    public NoteEntity addSingle (NoteEntity noteEntity) {
        if ( noteEntity == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        noteEntity.setLevel ( 1 );
        noteEntity.setCreateTime ( new Date () );
        int insert = baseMapper.insert ( noteEntity );
        if ( insert == 0 ) {
            throw new StateException ( StateEnum.INSERT_EXCE );
        }
        return noteEntity;
    }
}
